Addison Wesley : Patterns of Enterprise Application Architecture by By Martin Fowler David Rice Matthew Foemmel Edward Hieatt Robert Mee Randy Stafford

Addison Wesley : Patterns of Enterprise Application Architecture by By Martin Fowler David Rice Matthew Foemmel Edward Hieatt Robert Mee Randy Stafford

Author:By Martin Fowler, David Rice, Matthew Foemmel, Edward Hieatt, Robert Mee, Randy Stafford [By Martin Fowler, David Rice, Matthew Foemmel, Edward Hieatt, Robert Mee, Randy Stafford]
Language: eng
Format: epub
ISBN: 0321127420
Publisher: Addison Wesley
Published: 0101-01-01T00:00:00+00:00


Updates and Deletes

After all of that, updates and deletes are mostly harmless. Again we have an abstract method for the assumed usual case and an override for the special cases.

Updates work like this:

class AbstractMapper... public void update(DomainObjectWithKey subject) { PreparedStatement stmt = null; try { stmt = DB.prepare(updateStatementString()); loadUpdateStatement(subject, stmt); stmt.execute(); } catch (SQLException e) { throw new ApplicationException(e); } finally { DB.cleanUp(stmt); } } abstract protected String updateStatementString(); abstract protected void loadUpdateStatement(DomainObjectWithKey subject, PreparedStatement stmt) throws SQLException; class OrderMapper... protected void loadUpdateStatement(DomainObjectWithKey subject, PreparedStatement stmt) throws SQLException { Order order = (Order) subject; stmt.setString(1, order.getCustomer()); stmt.setLong(2, order.getKey().longValue()); } protected String updateStatementString() { return "UPDATE orders SET customer = ? WHERE id = ?"; } class LineItemMapper... protected String updateStatementString() { return "UPDATE line_items " + " SET amount = ?, product = ? " + " WHERE orderId = ? AND seq = ?"; } protected void loadUpdateStatement(DomainObjectWithKey subject, PreparedStatement stmt) throws SQLException { stmt.setLong(3, orderID(subject.getKey())); stmt.setLong(4, sequenceNumber(subject.getKey())); LineItem li = (LineItem) subject; stmt.setInt(1, li.getAmount()); stmt.setString(2, li.getProduct()); }

Deletes work like this:

class AbstractMapper... public void delete(DomainObjectWithKey subject) { PreparedStatement stmt = null; try { stmt = DB.prepare(deleteStatementString()); loadDeleteStatement(subject, stmt); stmt.execute(); } catch (SQLException e) { throw new ApplicationException(e); } finally { DB.cleanUp(stmt); } } abstract protected String deleteStatementString(); protected void loadDeleteStatement(DomainObjectWithKey subject, PreparedStatement stmt) throws SQLException { stmt.setLong(1, subject.getKey().longValue()); } class OrderMapper... protected String deleteStatementString() { return "DELETE FROM orders WHERE id = ?"; } class LineItemMapper... protected String deleteStatementString() { return "DELETE FROM line_items WHERE orderid = ? AND seq = ?"; } protected void loadDeleteStatement(DomainObjectWithKey subject, PreparedStatement stmt) throws SQLException { stmt.setLong(1, orderID(subject.getKey())); stmt.setLong(2, sequenceNumber(subject.getKey())); }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.